/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-05-08 16:11:33
 * @LastEditors: 'YSJ' '951866543@qq.com'
 * @LastEditTime: 2024-09-13 16:09:59
 */
import { defineStore } from 'pinia'
// import { hideAllByName} from '@/cesiumTools/material/effectController.js'
import { displayByName } from '../cesiumTools/material/effectController'

export const useLineData = defineStore('lineData', {
  state: () => {
    return {
      lineData: [],
      // 全局共享viewer，注意标记为markRaw
      viewer: null,
      tileset: null,
    }
  },
  getters: {
    // 获取全部信息
    allData() {
      return this.lineData
    },
    Viewer() {
      return this.viewer
    },
    Tile() {
      return this.tileset
    },
  },
  actions: {
    setData(data) {
      return new Promise((resolve, reject) => {
        this.lineData = data.length ? data : []
        resolve(data)
      })
    },
    setViewer(viewer) {
      return new Promise((resolve, reject) => {
        this.viewer = viewer
        resolve(viewer)
      })
    },
    setTileset(tileset) {
      return new Promise((resolve, reject) => {
        this.tileset = tileset
        resolve(tileset)
      })
    },
    controlItem(isChecked, id) {
      const line = this.lineData.find((line) => line.id === id)
      const { stationsList, name } = line
      const stationNames = stationsList.map((station) => station.name)

      displayByName(name, stationNames, !isChecked)
    },
    controlAll(isChecked) {
      const lineNames = this.lineData.map((line) => line.name)
      const stationNames = []
      this.lineData.forEach((line) => {
        const { stationsList } = line
        stationsList.forEach((station) => {
          stationNames.push(station.name)
        })
      })
      displayByName(lineNames, stationNames, isChecked)
    },
  },
})

// 由于lineData的state是异步的，所以需要监听action来获取数据
export const watchLineData = (actionName = 'setData') => {
  return new Promise((resolve, reject) => {
    useLineData().$onAction(({ name, store, args, after, onError }) => {
      if (name === actionName) {
        after((res) => {
          console.log(res)
          if (res) {
            resolve(res)
          }
        })

        onError((e) => {
          reject(e)
        })
      }
    })
  })
}

/* 处理站控措施的store */
export const useMeasureData = defineStore('measureData', {
  state: () => {
    return {
      measureData: [],
    }
  },
  getters: {
    // 获取全部信息
    allData() {
      return this.measureData
    },
  },
  actions: {
    setData(data) {
      return new Promise((resolve, reject) => {
        this.measureData = data.length ? data : []
        resolve(data)
      })
    },
    clearData() {
      return new Promise((resolve, reject) => {
        const data = this.measureData.map((item) => {
          item.length && item.forEach((n) => (n.measures.length = 0))
          return item
        })
        this.measureData = data
        resolve(data)
      })
    },
  },
})
// export const useMeasureData = defineStore('measureData', {
//   state: () => {
//     return {
//       lineData: [],
//       // 全局共享viewer，注意标记为markRaw
//       viewer: null,
//       tileset: null,
//     }
//   },
//   getters: {
//     // 获取全部信息
//     allData() {
//       return this.lineData
//     },
//     Viewer() {
//       return this.viewer
//     },
//     Tile() {
//       return this.tileset
//     },
//   },
//   actions: {
//     setData(data) {
//       return new Promise((resolve, reject) => {
//         this.measureData = data.length ? data : []
//         resolve(data)
//       })
//     },
//     setViewer(viewer) {
//       return new Promise((resolve, reject) => {
//         this.viewer = viewer
//         resolve(viewer)
//       })
//     },
//     setTileset(tileset) {
//       return new Promise((resolve, reject) => {
//         this.tileset = tileset
//         resolve(tileset)
//       })
//     },
//   },
// })
